package com.ygj.attendance.service.impl;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.base.common.persistent.dao.AttendanceRecordMapper;
import com.base.common.persistent.dao.SignPointMapper;
import com.base.common.persistent.model.AttendanceRecord;
import com.base.common.persistent.model.SignPoint;
import com.base.common.utils.EasyUtil;
import com.cjy.common.bean.Files;
import com.ygj.attendance.service.AttendanceService;

@Service("attendanceService")
public class AttendanceServiceImpl implements AttendanceService {

	@Resource
	private EasyUtil easyUtil;

	@Resource
	private AttendanceRecordMapper attendanceRecordMapper;
	
	@Resource
	private SignPointMapper signPointMapper;

	@Override
	public int attendance(AttendanceRecord attendanceRecord, String basePath,
			Files files, int type, int compoundsId) {

		if(type == 1) {
			SignPoint signPoint = new SignPoint();
			int signPointId = signPointMapper.querySignPointIdByCategoriesAndCompoundsId(3, compoundsId);
			signPoint.setId(signPointId);
			attendanceRecord.setSignPoint(signPoint);
		}
		
		// 保存图片
		String[] fileNames = files.getFileFileName();
		if (fileNames != null) {

			// 图片相对保存地址
			String sysImagesFile = "/resources/images/attendance" + "/"
					+ attendanceRecord.getEmployee().getId();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyyMMddHHmmss");
			String signTime = simpleDateFormat.format(attendanceRecord
					.getSignTime());
			for (int i = 0; i < fileNames.length; i++) {
				String[] contentTypes = fileNames[i].split("\\.");
				fileNames[i] = signTime + "-" + i + "."
						+ contentTypes[contentTypes.length - 1];
			}
			String picUrls = "";
			try {
				if (fileNames != null && fileNames.length != 0) {
					picUrls = easyUtil.savePictures(files.getFile(), basePath,
							sysImagesFile, fileNames, picUrls);
				}
				attendanceRecord.setImagesUrl(picUrls);
			} catch (IOException e) {
				e.printStackTrace();
				return 0;
			}

		}

		return attendanceRecordMapper.addAttendanceRecord(attendanceRecord);
	}

	@Override
	public List<AttendanceRecord> queryAttendanceRecordByMonth(int employeeId,
			int compoundsId, String queryTime) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
		Date date = dateFormat.parse(queryTime);

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);

		String day_first = df.format(calendar.getTime());
		StringBuffer str = new StringBuffer().append(day_first).append(
				" 00:00:00");
		day_first = str.toString();
		// System.out.println(day_first);

		calendar.add(Calendar.MONTH, 1); // 加一个月
		calendar.set(Calendar.DATE, 1); // 设置为该月第一天
		calendar.add(Calendar.DATE, -1); // 再减一天即为上个月最后一天

		String day_last = df.format(calendar.getTime());
		StringBuffer endStr = new StringBuffer().append(day_last).append(
				" 23:59:59");
		day_last = endStr.toString();

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date endTime = format.parse(day_last);
		Date startTime = format.parse(day_first);

		return attendanceRecordMapper.queryAttendanceRecordByMonth(employeeId,
				compoundsId, startTime, endTime);
	}

	@Override
	public Object manageQueryAttendanceRecord(int compoundsId, String queryTime) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
		Date date = dateFormat.parse(queryTime);

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);

		String day_first = df.format(calendar.getTime());
		StringBuffer str = new StringBuffer().append(day_first).append(
				" 00:00:00");
		day_first = str.toString();
		// System.out.println(day_first);

		calendar.add(Calendar.MONTH, 1); // 加一个月
		calendar.set(Calendar.DATE, 1); // 设置为该月第一天
		calendar.add(Calendar.DATE, -1); // 再减一天即为上个月最后一天

		String day_last = df.format(calendar.getTime());
		StringBuffer endStr = new StringBuffer().append(day_last).append(
				" 23:59:59");
		day_last = endStr.toString();

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date endTime = format.parse(day_last);
		Date startTime = format.parse(day_first);

		return attendanceRecordMapper.manageQueryAttendanceRecord(compoundsId, startTime, endTime);
	}

}
